package nju.bishe.weibo;

import java.util.List;

import weibo4android.WeiboException;


import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import nju.bishe.weibo.data.Position;
import nju.bishe.weibo.data.Tweet;
import nju.bishe.weibo.ui.base.Refreshable;
import nju.bishe.weibo.ui.base.WithHeaderActivity;
import nju.bishe.weibo.ui.module.MenuDialog;
import nju.bishe.weibo.util.CustomItemizedOverlay;

public class SignPageActivity extends MapActivity {
	private static final String TAG = "SignPageActivity";
	private static final String LAUNCH_ACTION = "nju.bishe.weibo.SignPageActivity";
	private Position currentLocation;
	private MapView mapView;
	private EditText statusText;
	TextView tv_current;
	TextView tv_sign;
	Button  signButton;
	private Button backButton;
	protected ImageButton refreshButton;
	protected ImageButton searchButton;
	protected ImageButton writeButton;
	protected ImageButton laughButton;
	
	protected void onCreate(Bundle savedInstanceState){
		Log.i(TAG, "onCreate()...");
		super.onCreate(savedInstanceState);

		requestWindowFeature(Window.FEATURE_NO_TITLE);
		requestWindowFeature(Window.FEATURE_PROGRESS);
		
		setContentView(R.layout.sign_page);
		initHeader();
		
		   
		currentLocation = getIntent().getParcelableExtra("positionId");
		tv_current = (TextView)findViewById(R.id.sign_current_location);
		tv_current.setText(currentLocation.getName());
		statusText = (EditText) findViewById(R.id.sign_info);
		signButton = (Button) findViewById(R.id.sign_btn);
		signButton.setOnClickListener(new OnClickListener() {
			
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					try {
						TwitterApplication.mApi.updateStatus(statusText.getText().toString(), Double.parseDouble(currentLocation.getLatitude()), Double.parseDouble(currentLocation.getLongitude()));
						
						//显示成功并返回
						Toast toast = Toast.makeText(getApplicationContext(),
							     "签到成功", Toast.LENGTH_LONG);
						toast.setGravity(Gravity.CENTER, 0, 0);
						toast.show();
						Intent intent=new Intent(SignPageActivity.this,TwitterActivity.class);	
						startActivity(intent);
					} catch (NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (WeiboException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			});
		    
		    
		    //init map
		    mapView = (MapView) findViewById(R.id.map_view);       
	        mapView.setBuiltInZoomControls(true);
	        
	        List<Overlay> mapOverlays = mapView.getOverlays();
	        Drawable drawable = this.getResources().getDrawable(R.drawable.icon);
	        CustomItemizedOverlay itemizedOverlay = 
	             new CustomItemizedOverlay(drawable, this);
	        
	        int latInt = (int)(Double.parseDouble(currentLocation.getLatitude())*1E6);
	        int longInt = (int)(Double.parseDouble(currentLocation.getLongitude())*1E6);
	        GeoPoint point = new GeoPoint(latInt,longInt);
	        OverlayItem overlayitem = 
	             new OverlayItem(point, "Hello", "I'm in Athens, Greece!");
	        
	        itemizedOverlay.addOverlay(overlayitem);
	        mapOverlays.add(itemizedOverlay);
	        
	        MapController mapController = mapView.getController();
	        
	        mapController.animateTo(point);
	        mapController.setZoom(12);
		
		
	}

    protected void initHeader() {
		addHeaderView(R.layout.header_back);
		
		//addBackButton();
		backButton = (Button) findViewById(R.id.top_back);
		backButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// Go back to previous activity
				finish();
			}
		});
		
		addRefreshButton();		
		addSearchButton();
		addWriteButton();
		addLaughButton();
	}


    private void addHeaderView(int resource) {
 	// find content root view
 	ViewGroup root =  (ViewGroup) getWindow().getDecorView();
 	ViewGroup content = (ViewGroup) root.getChildAt(0);
 	View header = View.inflate(SignPageActivity.this, resource, null);
// 	LayoutParams params = new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
 	
 	content.addView(header, 0);
 	content.removeViewAt(1);
     }

	@Override
	protected boolean isRouteDisplayed() {
		// TODO Auto-generated method stub
		return false;
	}

	private void addRefreshButton(){
		final Activity that = this;
		refreshButton = (ImageButton) findViewById(R.id.top_refresh);
		
		refreshButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// 旋转动画
				animRotate(v);
				
				if (that instanceof Refreshable) {
					((Refreshable) that).doRetrieve();
				} else {
					Log.e(TAG, "The current view " + that.getClass().getName() + " cann't be retrieved");
				}
			}
		});
	}
	
	protected void animRotate(View v) {
		if (null != v) {
			Animation anim = AnimationUtils.loadAnimation(v.getContext(),
					R.anim.rotate360);
			v.startAnimation(anim);
		}
	}

	// 搜索
	protected void addSearchButton() {
		searchButton  = (ImageButton) findViewById(R.id.search);
		searchButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startSearch();
			}
		});
	}
	
	// 这个方法会在SearchActivity里重写
	protected boolean startSearch() {
		Intent intent = new Intent();
		intent.setClass(this, SearchActivity.class);
		startActivity(intent);
		return true;
	}

	protected void addWriteButton() {
		writeButton = (ImageButton) findViewById(R.id.writeMessage);
		
		writeButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				// 动画
				Animation anim = AnimationUtils.loadAnimation(v.getContext(),
						R.anim.scale_lite);
				v.startAnimation(anim);
				startWrite();
				// forward to write activity
				
			}
		});
	}
	protected void startWrite() {
		Intent intent = new Intent();
		intent.setClass(this, WriteActivity.class);
		startActivity(intent);
	}

	protected void addLaughButton(){
		laughButton=(ImageButton)findViewById(R.id.laugh);
		laughButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(v.getContext(), LaughChannelActivity.class);
				v.getContext().startActivity(intent);
			}
		});
	}
}
